/*
 * Copyright 2012 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid.cell.longg;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.i18n.client.Messages;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.PopupPanel;
import finbudgettable.client.ui.grid.CellGrid;
import finbudgettable.client.ui.grid.cell.ContextMenu;
import finbudgettable.client.ui.grid.cell.ContextMenuPopupPanel;

public class LongContextMenu implements ContextMenu<Long> {

    public interface Strings extends Messages {

        @DefaultMessage("Меньше {0}")
        String less(String value);

        @DefaultMessage("Равно {0}")
        String eq(String value);

        @DefaultMessage("Не равно {0}")
        String neq(String value);

        @DefaultMessage("Больше {0}")
        String gt(String value);
    }

    final static Strings CONSTANTS = GWT.create(Strings.class);

    private final CellGrid grid;
    private final String fieldName;

    private MenuBar menu;
    private PopupPanel popupPanel;

    private final Command greaterCommand = new Command() {
        @Override
        public void execute() {
            grid.getFilter().setFilter(fieldName, new GreaterLongFilter(fieldName, currentValue));
            grid.filter();
            popupPanel.hide();
            grid.setFocus(true);
        }
    };

    private final Command lessCommand = new Command() {
        @Override
        public void execute() {
            grid.getFilter().setFilter(fieldName, new LessLongFilter(fieldName, currentValue));
            grid.filter();
            popupPanel.hide();
            grid.setFocus(true);
        }
    };

    private final Command equalCommand = new Command() {
        @Override
        public void execute() {
            grid.getFilter().setFilter(fieldName, new EqualLongFilter(fieldName, currentValue));
            grid.filter();
            popupPanel.hide();
            grid.setFocus(true);
        }
    };

    private final Command notEqualCommand = new Command() {
        @Override
        public void execute() {
            grid.getFilter().setFilter(fieldName, new NotEqualLongFilter(fieldName, currentValue));
            grid.filter();
            popupPanel.hide();
            grid.setFocus(true);
        }
    };

    public LongContextMenu(CellGrid grid, String fieldName) {
        this.grid = grid;
        this.fieldName = fieldName;
    }

    private void createMenu(Long value) {
        popupPanel = new ContextMenuPopupPanel(grid);
        menu = new MenuBar(true);
        menu.setAutoOpen(false);
        menu.addStyleName(ContextMenuPopupPanel.RESOURCES.style().bCellContextMenu());

        menu.addItem(CONSTANTS.less(value.toString()), lessCommand);
        menu.addItem(CONSTANTS.eq(value.toString()), equalCommand);
        menu.addItem(CONSTANTS.neq(value.toString()), notEqualCommand);
        menu.addItem(CONSTANTS.gt(value.toString()), greaterCommand);

        popupPanel.add(menu);
    }

    private Long currentValue;

    @Override
    public void show(int x, int y, Element parent, Long value) {
        currentValue = value;
        createMenu(value);
        popupPanel.setPopupPosition(x, y);
        popupPanel.show();
        menu.focus();
    }
}
